﻿using System;
using System.Text;

namespace jQueryCSharp.ui
{
    /// <summary>
    /// Classe Fluent para ui.Dialog
    /// </summary>
    public class ButtonBuilder : IjQueryCSharpUIBuilder<ButtonBuilder>
    {
        private const string TemplateDefault = "$(\"{0}{1}\").button({2});";
        private const string TemplateOthers = "$(\"{0}{1}\").buttonset({2});";
        private readonly Button _ui;
        private readonly jQueryCSharpUIBuilder _builder;
        private TípoBotao Type { get; set; }

        /// <summary>
        /// Construtor
        /// </summary>
        /// <param name="builder">jQueryCSharpUIBuilder</param>
        /// <param name="type"></param>
        public ButtonBuilder( jQueryCSharpUIBuilder builder, TípoBotao type )
            : this( builder, type, "" )
        {
        }

        /// <summary>
        /// Construtor
        /// </summary>
        /// <param name="builder">jQueryCSharpUIBuilder</param>
        /// <param name="type"></param>
        /// <param name="identifier"></param>
        public ButtonBuilder( jQueryCSharpUIBuilder builder, TípoBotao type, string identifier )
        {
            _ui = new Button( identifier );
            _builder = builder;
            Type = type;

            if ( type == TípoBotao.Toolbar )
            {
                Text( false );
            }
        }

        #region Métodos

        public ButtonBuilder ID( string value )
        {
            _ui.ID = value;
            return this;
        }

        public ButtonBuilder Class( string value )
        {
            _ui.Class = value;
            return this;
        }


        public ButtonBuilder Disabled( bool value )
        {
            _ui.Disabled = value;
            return this;
        }

        public ButtonBuilder Text( bool value )
        {
            _ui.Text = value;
            return this;
        }

        public ButtonBuilder IconPrimary( string value )
        {
            _ui.IconPrimary = value;
            return this;
        }

        public ButtonBuilder IconSecundary( string value )
        {
            _ui.IconSecundary = value;
            return this;
        }

        public ButtonBuilder Label( string value )
        {
            _ui.Label = value;
            return this;
        }

        public ButtonBuilder IsLink( bool value )
        {
            _ui.IsLink = value;
            return this;
        }

        public ButtonBuilder Click( string value )
        {
            _ui.Click = value;
            return this;
        }
        #endregion

        #region Override
        public new jQueryCSharpUIBuilder ToString( )
        {
            var sb = new StringBuilder( );

            var sinal = String.IsNullOrEmpty( _ui.Class ) ? "#" : ".";
            var id = String.IsNullOrEmpty( _ui.Class ) ? _ui.ID : _ui.Class;
            var template = String.Empty;

            switch ( Type )
            {
                case TípoBotao.Default:
                    template = TemplateDefault;
                    break;

                case TípoBotao.Radios:
                case TípoBotao.Checkboxes:
                    template = TemplateOthers;
                    break;
            }

            var templateButton = _ui.IsLink ? template.Substring( 0, template.Length - 1 ) : template;

            sb.AppendFormat( templateButton, sinal, id, _ui );

            if ( _ui.IsLink )
            {
                const string click = ".click(function() {{ {0} return false; }} );";
                sb.AppendFormat(click, String.IsNullOrEmpty(_ui.Click) ? "" : _ui.Click);
            }

            _builder.Append( sb.ToString( ) );

            return _builder;
        }
        #endregion
    }
}
